<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<script type="text/javascript">
	Ext.onReady(function() {

		var insStore = new Ext.data.JsonStore({
			totalProperty: 'total', 
			root: 'results', 
			sortInfo: {field: 'insName', direction: 'ASC'},
			remoteSort: true,
			proxy : new Ext.data.HttpProxy({
				url: '<?=site_url()?>admin/get_institute/0/FAC/FAC',
				method: 'GET'
			}),
			fields: [
				{name: 'insId'},
				{name: 'insName'},
				{name: 'insUrlName'},
				{name: 'insCityId'},
				{name: 'insPriority'},
				{name: 'cityName'},
				{name: 'couName'},
				{name: 'conName'},
				{name: 'cityUrlName'},
				{name: 'couUrlName'},
				{name: 'conUrlName'},
				{name: 'insLatitude'},
				{name: 'insLongitude'},
				{name: 'insImageNameThumb'},
				{name: 'insImageAlt'},
				{name: 'insInsId'},
				{name: 'parentInsName'},
				{name: 'childInsName'},
				{name: 'e2cTempFlag'},
				{name: 'insPresenter'}
			]
		});

		var conContextMenuIns = new Ext.menu.Menu({
			items: [{
				id: 'majFilterIns',
				iconCls: 'icon-zoom',
				text: ''
			}],
			listeners: {
				itemclick: function(item) {
					switch (item.id) {
						case 'majFilterIns': {
							Ext.getCmp('pagePanel').findById('majGrid').expand();
							Ext.getCmp('majGrid').getStore().load({params: {fields:'["insName"]', query: Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName}});
						} break;
					}
				}
			}
		});

		var facGrid = new Ext.grid.GridPanel({
			id: 'facGrid',
			store: insStore,
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>',
				getRowClass: function(record, index) {
					if(record.data.e2cTempFlag == 'I') return 'temp_data_exists';
					else if(record.data.insPresenter == 'I') return 'presenter_live';
					else return '';
				}
			},
			columns: [
				{id: 'insName', header: '<?=$this->db->escape_str($this->lang->line("INSTITUTE"))?>', width: 200, sortable: true, dataIndex: 'insName', renderer: function(value, cell, record) {
					return '' + value + '<br/>' +
										((record.data.parentInsName)?('<div style="padding-left: 10px; font-style: italic;"><?=$this->db->escape_str($this->lang->line("PARENT"))?>: ' + record.data.parentInsName + '</div>'):('')) +
										((record.data.childInsName)?('<div style="padding-left: 10px; font-style: italic;"><?=$this->db->escape_str($this->lang->line("CHILD"))?>: ' + record.data.childInsName + '</div>'):(''));
				}},
				{header: '<?=$this->db->escape_str($this->lang->line("COUNTRY"))?>', width: 150, sortable: true, dataIndex: 'couName'},
				{header: '<?=$this->db->escape_str($this->lang->line("CITY"))?>', width: 150, sortable: true, dataIndex: 'cityName'}
			],
			stripeRows: true,
			autoExpandColumn: 'insName',
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),

			listeners: {

				rowdblclick: function(grid, rowIndex, event) {
					if(rowIndex != 'new') {
						var record = grid.getStore().getAt(rowIndex);
					} else {
						var record = new Object();
						record.data = new Object();
					}
					DataEdit.showWindow('FAC', ((rowIndex != 'new')?(''):('-')) + record.data.insId)
				}

				,rowcontextmenu: function(grid, index, event) {
					event.stopEvent();
					if(Ext.getCmp('facGrid').getSelectionModel().getSelections().length > 0) {
						Ext.getCmp('majFilterIns').setText('<?=$this->db->escape_str($this->lang->line("REFINE_THE_COURSES_WITH_THE_SELECTED_ITEM"))?> - ' + Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName);
						conContextMenuIns.showAt(event.xy);
					}
				}

			},
			border: false,
			frame: false,

			tbar: {
				items: [
					<? if(Common::isAdminJog("INS")) { ?>
					{
						icon: '<?=MEDIA?>icon/application_form_add.png',
						text: '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>',
						handler: function(){
							DataEdit.showWindow('FAC', -1);
						}
					}, {
							xtype: 'tbseparator'
					},
					{
						icon: '<?=MEDIA?>icon/application_form_edit.png',
						text: '<?=$this->db->escape_str($this->lang->line("EDIT"))?>',
						ref: '../editBtn',
						disabled: true,
						handler: function() {
							Ext.getCmp('facGrid').fireEvent('rowdblclick', Ext.getCmp('facGrid'), Ext.getCmp('facGrid').store.indexOf(Ext.getCmp('facGrid').getSelectionModel().getSelected()), new Object());
						}
					}, {
							xtype: 'tbseparator'
					},
					<? } ?>
					{
						icon: '<?=MEDIA?>icon/application_form_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("DELETE"))?>',
						ref: '../removeBtn',
						disabled: true,
						handler: function() {
							DataEdit.delete('FAC', Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insId, Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_preview.png',
						text: '<?=$this->db->escape_str($this->lang->line("PREVIEW"))?>',
						ref: '../previewBtn',
						disabled: true,
						handler: function(){
							window.open('<?=site_url()?>preview/institutes/' + Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insUrlName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish.png',
						text: '<?=$this->db->escape_str($this->lang->line("PUBLICATION"))?>',
						ref: '../publishBtn',
						disabled: true,
						handler: function(){
							DataEdit.publish('FAC', Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insId, Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName, 1);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA"))?>',
						ref: '../publishBtnDelete',
						disabled: true,
						handler: function(){
							DataEdit.publish('FAC', Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insId, Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName, 0);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_remove.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_PUBLISHED_DATA"))?>',
						ref: '../unpublishBtn',
						disabled: true,
						handler: function(){
							DataEdit.unpublish('FAC', Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insId, Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insName, 2);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/image.png',
						text: '<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>',
						ref: '../imageBtn',
						disabled: true,
						handler: function(){
							DataEdit.image('FAC', Ext.getCmp('facGrid').getSelectionModel().getSelections()[0].data.insId);
						}
					}
				]
			},

			bbar: 
				new Ext.PagingToolbar({
					pageSize: <?=$this->config->item("adminGridPageSize")?>,
					store: insStore,
					displayInfo: true,
					plugins: new Ext.ux.ProgressBarPager()
				}),

			plugins:[
				new Ext.ux.grid.Search({
					minChars:2,
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:150,
					disableIndexes:[],
					autoFocus:true
				})]

		});

		Ext.getCmp('facGrid').render('grid-table-fac');

		Ext.getCmp('facGrid').getSelectionModel().on('selectionchange', function(sm){
			Ext.getCmp('facGrid').removeBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('facGrid').editBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('facGrid').publishBtn.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('facGrid').publishBtnDelete.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('facGrid').unpublishBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('facGrid').previewBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('facGrid').imageBtn.setDisabled(sm.getCount() < 1);
		});

		insStore.load({params:{start:0, limit:<?=$this->config->item("adminGridPageSize")?>}});


	})
</script>


<div id="grid-table-fac"></div>